﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Webcam.Image
{
    /// <summary>
    /// Modifying graphics
    /// </summary>
    public static class GraphicsDrawing
    {
        /// <summary>Draw a line</summary>
        /// <param name="co">The original image</param>
        /// <param name="p1">Point 1</param>
        /// <param name="p2">Point 2</param>
        /// <param name="color">Color of the line</param>
        public static void DrawLine(RGBImage co, Point p1, Point p2, RGB color)
        {
            Point[] points = Maths.PointsAlongLine(p1, p2);

            foreach (Point p in points)
            {
                co[p] = color;
            }
        }

        /// <summary>Draw a line</summary>
        /// <param name="co"></param>
        /// <param name="l"></param>
        public static void DrawLine(RGBImage co, Line l)
        {
            DrawLine(co, l.p1, l.p2, l.Color);
        }

        /// <summary>Draw some lines</summary>
        /// <param name="co"></param>
        /// <param name="lines"></param>
        public static void DrawLine(RGBImage co, Line[] lines)
        {
            foreach (Line l in lines)
            {
                DrawLine(co, l);
            }
        }


        /// <summary>Draw a dot</summary>
        /// <param name="c">The color data</param>
        /// <param name="p">The top left corner of the point</param>
        /// <param name="width">The width of the point</param>
        /// <param name="color">The color of the point</param>
        public static void DrawPoint(RGBImage c, Point p, int width, RGB color)
        {
            int right = p.X + width;
            if (right > c.Width)
                right = c.Width;
            int bottom = p.Y + width;
            if (bottom > c.Height)
                bottom = c.Height;

            for (int y = p.Y; y < bottom; y++)
            {
                for (int x = p.X; x < right; x++)
                {
                    c[x,y] = color;
                }
            }
        }




        /// <summary>Draw a solid box</summary>
        /// <param name="co"></param>
        /// <param name="rect"></param>
        /// <param name="color"></param>
        public static void FillBox(RGBImage co, Rectangle rect, RGB color)
        {
            Point topLeft = rect.TopLeft;
            Point bottomRight = rect.BottomRight;

            for (int y = topLeft.Y; y <= bottomRight.Y; y++)
            {
                for (int x = topLeft.X; x <= bottomRight.X; x++)
                {
                    co[x,y] = color;
                }
            }
        }

        /// <summary>Draw a grid</summary>
        /// <param name="co"></param>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        public static void DrawGrid(RGBImage co, int Width, int Height)
        {
            for (int x=0;x<co.Width;x+=Width)
            {
                DrawLine(co, new Point(x, 0), new Point(x, co.Height - 1), RGB.White);
            }
            for (int y = 0; y < co.Height; y += Height)
            {
                DrawLine(co, new Point(0, y), new Point(co.Width - 1, y), RGB.White);
            }
        }

    }
}
